<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>地图</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" href="../css/mui.min.css" />
		<link rel="stylesheet" href="../css/main.css" />
		<link rel="stylesheet" href="../css/mui.picker.css" />
		<link rel="stylesheet" href="../css/mui.poppicker.css" />
		<link rel="stylesheet" href="../css/feedback.css" />
		<!--图标-->
		<link rel="stylesheet" type="text/css" href="../css/icons-extra.css" />

		<style>
			#allmap {
				width: 100%;
				height: 100%;
				overflow: hidden;
				margin: 0;
				font-family: "微软雅黑";
				margin-top: 5px;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title" id="ptitle">地图</h1>
		</header>
		<div class="mui-content" id="content" style="height: 600px;">
			<div id="allmap">
				地图加载中...
			</div>
		</div>
	</body>
	<script type="text/javascript" src="../js/mui.min.js"></script>
	<script type="text/javascript" src="../js/app.js"></script>
	<script type="text/javascript" src="../js/constants.js"></script>
	<script type="text/javascript" src="../js/xiangyingshi.js"></script>
	<script type="text/javascript" src="../js/jquery-3.1.1.min.js"></script>
	<script type="text/javascript" src="../plugs/moment/moment.min.js"></script>
	<script>
		mui.init({
			keyEventBind: {
				backbutton: true //打开back按键监听
			},
			beforeback: function() {
				//返回true，继续页面关闭逻辑  
				mui.back;
				return true;
			}
		});
		
	
		var getordergpsUrl = serverAddress + "/api/dc/order/getordergps/"
		var orderGps=[];
		function getRoute() {
			commonHttpUtils(getordergpsUrl + order_id, "post", {
				serviceNumber: serviceNumber
			}, sussess, error);
		}
		mui.plusReady(function() {
			
			var self = plus.webview.currentWebview();
			serviceNumber = self.SERVICENUMBER;
			order_id=self.ORDER_ID;
			orderGps=self.ORDERGPS;
			var currentView = plus.webview.currentWebview();
			currentView.show('slide-in-right', 200);
			plus.nativeUI.closeWaiting();

			if(plus.navigator.isImmersedStatusbar()) { // 兼容immersed状态栏模式
				// 获取状态栏高度并根据业务需求处理，这里重新计算了子窗口的偏移位置
				topoffset = (Math.round(plus.navigator.getStatusbarHeight()) + 45) + 'px';
				document.querySelector("header").style.height = topoffset;
				document.querySelector("header").style.paddingTop = "20px";
				document.querySelector(".mui-bar-nav~.mui-content").style.paddingTop = topoffset;
			}
			var map = new plus.maps.Map('allmap');
			map.centerAndZoom(new plus.maps.Point(parseFloat(orderGps.coordinates.toString().split(",")[0]), parseFloat(orderGps.coordinates.toString().split(",")[1])), 15);
			var sussess = function(data) {
				console.log("3222"+JSON.stringify(data));
				if(data.result == "success" && data.obj[0] && data.obj[0].ingGps && data.obj[0].ingGps.length > 0) {
					var startp = data.obj[0].ingGps[data.obj[0].ingGps.length - 1]
					var searchObj = new plus.maps.Search(map);
					searchObj.onRouteSearchComplete = function(state, result) {
						if(state == 0) {
							if(result.routeNumber <= 0) {
								mui.toast('1路径加载失败，请稍后再试！');
							}
							var route = result.getRoute(0); //取第一个路径
							var sd = 666; //米/min
							if(data.obj[0].ingGps.length > 5) {
								var startPoint = data.obj[0].ingGps[0]
								getDistance(map, new plus.maps.Point(startPoint.longitude, startPoint.latitude), new plus.maps.Point(startp.longitude, startp.latitude), function(distance) {
									console.log("888", distance, moment(startPoint.date), moment(startp.date))
									//moment(startPoint.date)
									var min = Math.abs(moment(startp.date).diff(moment(startPoint.date), 'minutes'));
									var csd = parseInt(distance / min)
									if(csd > 333) {
										sd = csd;
									}
									getMarker(map, route, sd, startp)
								})
							} else {
								getMarker(map, route, sd, startp)
							}
						} else {
							mui.toast('3路径加载失败，请稍后再试！');
						}
					}
					searchObj.drivingSearch(new plus.maps.Point(startp.longitude, startp.latitude), "长春", new plus.maps.Point(parseFloat(orderGps.coordinates.toString().split(",")[0]),parseFloat(orderGps.coordinates.toString().split(",")[1])), "长春");
				} else {
					mui.toast('医生还没有出发！');
				}
			}

			function getMarker(map, route, sd, startp) {
				map.clearOverlays()
				map.addOverlay(route);
				var distance = route.distance
				marker = new plus.maps.Marker(new plus.maps.Point(startp.longitude, startp.latitude));
				marker.setIcon("../images/dong.gif");
				marker.setLabel("小7还有" + (distance / sd).toFixed(2) + "分钟到达");
				map.addOverlay(marker);
				
//				var map = new BMap.Map("allmap");
//				var point = new BMap.Point(startp.longitude, startp.latitude);
//				map.centerAndZoom(point, 15);
//	
//				//创建小狐狸
//				var pt = new BMap.Point(startp.longitude, startp.latitude);
//				var myIcon = new BMap.Icon("http://lbsyun.baidu.com/jsdemo/img/fox.gif", new BMap.Size(300,157));
//				var marker2 = new BMap.Marker(pt,{icon:myIcon});  // 创建标注
//				map.addOverlay(marker2);              // 将标注添加到地图中
//				var myIcon = new plus.maps.Icon("../images/xiao7.png", new plus.maps.size(300,157));
//				var marker2 = new plus.maps.Marker(new plus.maps.Point(startp.longitude, startp.latitude),{icon:myIcon});  // 创建标注
//				marker2.setLabel("小7还有" + (distance / sd).toFixed(2) + "分钟到达");
//				map.addOverlay(marker2);
				
				
				
			}

			var err = function() {
				mui.toast('222路径加载失败，请稍后再试！');
			}
			commonHttpUtilsNoWaiting(getordergpsUrl + order_id, "post", {
				serviceNumber: serviceNumber
			}, sussess, err);
			window.setInterval(function() {
				commonHttpUtilsNoWaiting(getordergpsUrl + order_id, "post", {
					serviceNumber:serviceNumber
				}, sussess, err);
			}, 10000);

			function getDistance(map, start, end, fc) {
				var searchObj = new plus.maps.Search(map);
				searchObj.onRouteSearchComplete = function(state, result) {
					var distance = 0;
					if(state == 0) {
						var route = result.getRoute(0);
						distance = route.distance
					}
					fc(distance)
				}
				searchObj.drivingSearch(start, "长春", end, "长春");
			}
		});
	</script>
</html>